package com.nmgsr.ptmsapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class QRCodeScannerActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 初始化扫描器
        IntentIntegrator integrator = new IntentIntegrator(this);
        integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE);
        integrator.setPrompt("扫描设备二维码");
        integrator.initiateScan();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                Toast.makeText(this, "扫描取消", Toast.LENGTH_LONG).show();
            } else {
                String qrCodeContent = result.getContents();
                // 处理解析后的二维码内容
                handleQRCodeContent(qrCodeContent);
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    private void handleQRCodeContent(String qrCodeContent) {
        // 这里可以添加解析二维码内容的逻辑
        // 例如，解析设备信息并添加设备
        Toast.makeText(this, "扫描结果: " + qrCodeContent, Toast.LENGTH_LONG).show();
        // 返回扫描结果到 LoginActivity
        Intent intent = new Intent();
        intent.putExtra("qrCodeContent", qrCodeContent);
        setResult(RESULT_OK, intent);
        finish();
    }
}
